package com.sqsf.util;

import com.sqsf.javabean.Role;
import mapper.RoleMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class RoleTest {
    SqlSession session;
    RoleMapper mapper;
    @Before
    public void a() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper= session.getMapper(RoleMapper.class);
    }

    @After
    public void b() {
        //5、关闭资源
        session.close();
    }
    @Test
    public void selectRole_Permission(){
        for (Role role : mapper.selectRole_Permission()) {
            System.out.println(role);
        }
    }
    @Test
    public void selectRole_Permission_lazy(){
        List<Role> roles = mapper.selectRole_Permission_lazy();
        Role r1 = roles.get(0);
        System.out.println(r1.getRid()+" "+r1.getName());
        Role r2 = roles.get(1);
        System.out.println(r2.getRid()+" "+r2.getName());
        System.out.println("如果仅使用role对象数据 不会触发加载");
        System.out.println(r2.getPlist());
        System.out.println("如果使用了权限数据，仅加载查询数据");
    }
}